package com.sidianzhong.sdz.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.model.WebEquipmentonoffrecord;
import com.sidianzhong.sdz.service.WebEquipmentonoffrecordService;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "设备开关机记录表")
@Controller
public class WebEquipmentonoffrecordController {

    @Autowired
    WebEquipmentonoffrecordService webEquipmentonoffrecordService;

    @Autowired
    HttpServletResponse response;

    @ApiOperation(value = "批量创建开关机时间")
    @RequestMapping(value = "/batchInsertTime",
                    method = RequestMethod.POST,
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createWebEquipmentonoffrecord(
            HttpServletRequest request,
            @RequestParam(value = "json", required = false) String json
    ) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        String userName = jsonObject.getString("userName");
        Date date = new Date();
        JSONArray data = jsonObject.getJSONArray("data");
        List<WebEquipmentonoffrecord> collect = data.stream().map(ss -> {
            String s1 = JSONObject.toJSONString(ss);
            JSONObject jsonObject1 = JSONObject.parseObject(s1);
            WebEquipmentonoffrecord item = new WebEquipmentonoffrecord();
            item.setEquipmenttypekey(jsonObject1.getString("equipmenttypekey"));
            item.setEquipmentkey(jsonObject1.getString("equipmentkey"));
            item.setTurnonofftime(jsonObject1.getDate("turnonofftime"));
            item.setOperatingtype(jsonObject1.getInteger("operatingtype"));
            item.setRecordtime(date);
            item.setStaffname(userName);
            return item;
        }).collect(Collectors.toList());
        int i = webEquipmentonoffrecordService.batchInsert(collect);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("result",true);
        return new ResponseEntity<>(ResultModel.ok(jsonObject1), HttpStatus.OK);
    }

    @ApiOperation(value = "查询该设备开关机时间")
    @RequestMapping(value = "/getOpenTime",
                    method = {RequestMethod.GET},
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getOpenTime(
            HttpServletRequest request,
            @RequestParam(value = "equipmenttypekey", required = false) String equipmenttypekey,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey
    ) {

        JSONObject jsonObject = webEquipmentonoffrecordService.setlectTime(equipmenttypekey, equipmentkey);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);

    }

}
